Similar to Boostrap, we can dowland the JQuery, or we can add it with the CDN. Naturaly the CDN have some ventajes over dowland the file. Minify: it's trying to reduce a file size, to the minus code, this process is deleting all the breaklines, spaces, comments or somethingn innecesary code or character from our code.
The CDN
<script src="https://code.jquery.com/jquery-3.6.4.min.js" integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script>
In the file.html
<!-- JQuery -->
<script src="https://code.jquery.com/jquery-3.6.4.js" integrity="sha256-a9jBBRygX1Bh5lt8GZjXDzyOB+bWve9EiO7tROUtj/E=" crossorigin="anonymous"></script>
<!-- own js -->
<script src="./index.js"></script>
for using the select in jquery, we just need the dollar sign "$": Hello there i'm a string
//------- with JQUERY
$(".select-btn").each((index, element)=>{
---code
$(".text1").text("--code");
//------- WITH JS
document.querySelectorAll(".select-btn").forEach((element, index) => { --code
document.querySelector(".text1").textContent --code
Toching the CSS, for that we just add the css method, and pass througth: first, the css property, second, the value for that css property
//-------Set the css value
pass the atribute as the first paramter, and, the value as the second parameter
$(".title-2").css("color",randomRgb());
//-------Get the css value
pass the atribute only, without the value
alert("Color setted "+ $(".title-2").css("color"));
//-------Add a class
$(".title-2").addClass("big-title italic");
//-------Has a class?
alert("Has big title? : "+$(".title-2").hasClass("big-title"));
//-------remove a class?
$(".title-2").removeClass("big-title");
Toching the textContent, for that we just add the text() method, and pass througth as a parameter the whised text.
To get the textContetn, don't pass any parameter
Instance we can use html() method its equivalent to say innerHTML
with parameter we set the value, with any parameter we get the value
passing the whised text as a paramneter
//-------Set text with text()
$(".title-3").text("Hi <i>"+name+" </i>");
//-------Set text with html()
$(".title-3").html("Hi <i>"+name+" </i>");
We can add or change atributes by the function .prop("property","valueIfWantedToChange")
passing the whised value as a the second paramneter
let img = $(".img-1")
//-------Set src atribute
img.prop("src","pineapple.png");;
img.prop("src","tomato.png");;
//-------Set src atribute
let disabled = $(".btn-test").prop("disabled"); //-- method called without the second parameter, so it's return the value with any change
$(".btn-test").prop("disabled",(disabled ? false : true));
We can add event listeners with the method .on("event",callBackFunction)
//-------SAY HI
$(".modify-event-btn").on("click",function () {
alert("Hi, my clases are:"+$(this).prop("class"));
});
//-------disabled button (this button is above, in the Manipulating atributes )
$($(".modify-atributes-btn")[0]).on("click",function () {
let disabled = $(".btn-test").prop("disabled");
$(".btn-test").prop("disabled",(disabled ? false : true));
});
// KEY DOWN DETECTER
$(document).on("keydown",function (event) {
let actualText = $(".label-1").text();
if(actualText.length >21){
actualText = actualText.slice(1,22)
$(".label-1").text(actualText)
};
$(".label-1").text(actualText + event.key)
});
We can add or delete new elements, in current serverd page, for that:
//-------ADD ELEMENTS
function addElement() {
$(".add-before-me").before("<button type='button' class='btn-added' onclick='hi()'>I'm a new button</button>");
};
//-------REMOVE ELEMENTS
function removeBtn() {
let btns = $(".btn-added")
let len = btns.length
if (len === 0) {
alert("There's not any bnt-new to delete");
}else{
$(btns[len-1]).remove();
};
};
W3schools documentation about jquery effect methods.
With JQuery we can make lots of effects.
//-------FADE TOOGLE
function imgFadeToogle() {
$(".img-2").fadeToggle();
};
//-------SLIDE TOOGLE
function imgSlideToogle() {
$(".img-2").slideToggle();
}: